/* 
 * File:   CoordinatesInverter.cpp
 * Author: Guilherme Monteiro
 */

#include "../headers/CoordinatesInverter.h"

/**
 * Constructor of class CoordinatesInverter
 * 
 * @param v: vector with points to invert: points[0] = leftUpper and points[1] = rightBottom
 * @param originalImg: original image rotated by the system.
 */
CoordinatesInverter::CoordinatesInverter(vector<CvPoint> v, IplImage* originalImg) throw (NullImageException) {
    this->points = v;
    setImg(originalImg);
}

CoordinatesInverter::CoordinatesInverter(const CoordinatesInverter& orig) {
}

CoordinatesInverter::~CoordinatesInverter() {
}

/**
 * Sets image to works.
 * 
 * @param img: originalImg
 */
void CoordinatesInverter::setImg(IplImage* img) throw (NullImageException) {
//    if(img == 0) {
//        throw NullImageException();
//    }
    this->img = img;
}

/**
 * Invert points read of input file, is used
 * when the image is rotated in 90 degrees.
 * 
 * @return vector of invertedPoints: invertedPoints[0] = newleftUpper and 
 *  invertedPoints[1] = newrightBottom
 */
vector<CvPoint> CoordinatesInverter::invertPoints() {
    vector<CvPoint> invertedPoints;
    vector<CvPoint>::iterator it;
    for(it = points.begin(); it != points.end(); it++) {
        invertedPoints.push_back(cvPoint(it->y, it->x));
    }
    
//    printf("points[0].x: %d\n", points[0].x);
//    printf("points[0].y: %d\n", points[0].y);
//    printf("points[1].x: %d\n", points[1].x);
//    printf("points[1].y: %d\n", points[1].y);
    
//    printf("img width: %d\n", img->width);   // width  = 3399 
//    printf("img heigth: %d\n", img->height); // heigth = 2406
    
//    cvReleaseImage(&image);
    
    invertedPoints[0].x = img->width - points[1].y; // invertedPoints[0].x = 3399 - 3084 = 315
    invertedPoints[0].y = points[0].x;              // invertedPoints[0].y = 252
    invertedPoints[1].x = img->width - points[0].y; // invertedPoints[1].x = 3399 - 272 = 3127
    invertedPoints[1].y = points[1].x;              // invertedPoints[1].y = 2180
    
//    printf("\ninvertedPoints[0].x: %d\n", invertedPoints[0].x);
//    printf("invertedPoints[0].y: %d\n", invertedPoints[0].y);
//    printf("invertedPoints[1].x: %d\n", invertedPoints[1].x);
//    printf("invertedPoints[1].y: %d\n", invertedPoints[1].y);
    
    return invertedPoints;
}
